import service from "@/request";

interface DeviceDataI {
    udid: string
    name: string
    imei: string
    status: string
}

export function apiGetAllDevices() {
    return service.request({
        // url: 'api/devices/list',
        url: 'api/cases',
        method: 'get',
        params: {
            getAll: true
        }
    })
}

export function apiGetDeviceDetail(id: number) {
    return service.request({
        url: 'api/devices/info/' + id,
        method: 'GET',
    })
}

export function apiGetDeviceList(filter = {}) {
    return service.request({
        url: 'api/devices/list',
        method: 'GET',
        params: {
            ...filter,
        }
    }).then(
        res => {
            if ((!res.pageSize) && ("pageSize" in filter)) {
                res.pageSize = filter.pageSize
            }
            if ((!res.page) && ("page" in filter)) {
                res.page = filter.page
            }
            return res;
        }
    ).catch(error => {
        console.error("API error: ", error); // 捕获并打印错误
        throw error; // 重新抛出错误，以便调用者可以处理
    });
}


export function apiAddDevice(device) {
    return service.request({
        url: 'api/devices/list',
        method: 'POST',
        data: device
    })
}

export function apiUpdateDevice(device: DeviceDataI) {
    return service.request({
        url: 'api/devices/list',
        method: 'PUT',
        data: device
    })
}

export function apiDeleteDevice(id: number) {
    return service.request({
        url: 'api/devices/delete/' + id,
        method: 'DELETE',
        params: {
            id: id
        }
    })
}

export function apiGetDeviceStatuses() {
    return service.request({
        url: 'api/devices/statuses',
        method: 'GET',
    })
}